package com.lt.flashcard.view;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.Repository;

/**
 * Render the list learned flashcardset at dashboard
 * 
 * @author ocsen
 * 
 */
public class DashboardLearnedListVm {

    private List<FlashcardSet> list = null;
    public static final int PAGE_SIZE = 12;
    private int page = 0;
    private long total = 0;

    private User user;// current user
    private DashboardCtrl ctrl = null;

    @Init
    public void init(@ContextParam(ContextType.COMPONENT) Component com) {
        ctrl = (DashboardCtrl) com.getRoot().getFellow("dbctrl").getAttribute("$composer");

        this.user = ctrl.getUser();
        LearningService srv = Repository.getInstance().getLearningService();
        total = srv.countLastTestedFlashcardsets(getUser());
        loadList();

        // if(list != null && list.size() > 0 && ctrl.isOwner())
        // Clients.evalJavaScript("$.display(1)");
    }

    @NotifyChange("list")
    @Command
    public void paging(@BindingParam("event") PagingEvent event) {
        page = event.getActivePage();
        loadList();
    }

    public void loadList() {
        LearningService srv = Repository.getInstance().getLearningService();
        setList(srv.getLastTestedFlashcardsets(getUser(), page, PAGE_SIZE));
    }

    public int getNumForTesting(FlashcardSet fcs) {
        FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
        return srv.getNumOfWaitingWordForTest(fcs);
    }

    public int getNumKnowCards(FlashcardSet fcs) {
        FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
        return srv.getNumOfKnowCards(fcs);
    }

    /**
     * @return the user
     */
    public User getUser() {
        return user;
    }

    /**
     * @param user
     *            the user to set
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * @return the list
     */
    public List<FlashcardSet> getList() {
        return list;
    }

    /**
     * @param list
     *            the list to set
     */
    public void setList(List<FlashcardSet> list) {
        this.list = list;
    }

    /**
     * @return the total
     */
    public long getTotal() {
        return total;
    }

    /**
     * @param total
     *            the total to set
     */
    public void setTotal(long total) {
        this.total = total;
    }
}